﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class EnumSignature : Declaration
{
    private readonly SyntaxList<EnumMemberSignature> _members;

    internal EnumSignature(SyntaxToken enumKeywordToken, Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.EnumSignature, enumKeywordToken, name, parent)
    {
        _members = new SyntaxList<EnumMemberSignature>();
    }


    public override bool IsSignature => true;

    public override bool IsTypeSignature => true;


    /// <summary>
    ///     此枚举声明所在的模块定义
    /// </summary>
    public ModuleSignature? ContainingModule { get; internal set; }


    /// <summary>
    ///     枚举成员列表
    /// </summary>
    public IReadOnlyList<EnumMemberSignature> Members => _members;


    protected override string GetFullName()
    {
        if (Parent is ModuleSignature module)
        {
            return $"{module.FullName}.{Name}";
        }

        return Name;
    }


    /// <summary>
    ///     添加一个新的枚举成员
    /// </summary>
    public void AddMember(EnumMemberSignature member)
    {
        AddChildMemberToReadOnlyField(_members, member);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        base.AcceptVisitor(visitor);
        AcceptList(visitor, Members);
    }
}


public static partial class NodeFactory
{
    public static EnumSignature CreateEnumSignature(SyntaxNode? parent, SyntaxToken enumKeywordToken,
        Identifier identifierName)
    {
        Debug.Assert(enumKeywordToken.IsNode(SyntaxKinds.EnumKeywordToken));
        return new EnumSignature(enumKeywordToken, identifierName, parent);
    }
}